//
//  TJWelcomeViewController.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/12.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class TJWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        self.setValueForUI()
        // 加载用户信息.
        TJAccountViewModel.shareUserAccount.loadUserInfo { (error) -> () in
            if error != nil {
            
                TJPrint("加载用户信息失败\(error)")
                return
            }
            
//            TJPrint("加载用户信息成功")
            // 给控件赋值.
            self.setValueForUI()
        }
    }
    
    // 给控件赋值.
    private func setValueForUI() {
    
        if let imgURLString = TJAccountViewModel.shareUserAccount.userAccount?.avatar_large,let nameText = TJAccountViewModel.shareUserAccount.userAccount?.screen_name {
        let url = NSURL(string: imgURLString)
        self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        self.welcomeLel.text = nameText
        }

    }
    
    // MARK:私有方法.
    private func setupUI(){
    
        // 1,添加子控件.
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLel)
        
        // 2,设置约束.
        // 禁用Autoresizeing.
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景.
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        
        // 头像.
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-150)
            make.width.equalTo(85)
            make.height.equalTo(85)
        }
        
        // label.
        welcomeLel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        addAnimation()
    }
    
    // 给头像加动画,在界面完全显示出来的时候调用.
    @objc private func addAnimation(){
    
        // 更新iconView的约束.
        self.iconView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 150))
        })
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 显示label.
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.welcomeLel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 显示完成.切换到主界面.
                       (UIApplication.sharedApplication().delegate as! AppDelegate).changeWindowRootVC(TJTabBarController())
                })
        }
    
    }
    
    // MARK: 懒加载子控件.
    // 背景.
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像.
    private lazy var iconView:UIImageView = {
    
        let iconImgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 裁剪为圆形图片.-->1X图片素材宽度的一般.
        iconImgView.layer.cornerRadius = 42.5
        iconImgView.layer.masksToBounds = true
        return iconImgView
    }()
    
    // 欢迎label.
    private lazy var welcomeLel:UILabel = {
    
        let label = UILabel()
        // 设置label属性.
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(TJWelcomeLabelFontSize)
        label.sizeToFit()
        
        return label
    }()

    

}
